package controler;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import castleContent.User;
import castleContent.Users;

@WebServlet("/MapServlet")
public class MapServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User attacker = (User) session.getAttribute("Object");
		String name;
		while(true){
			name = getEnemyUser().getName();
			if(attacker.getName().equals(name)){
				continue;
			} else{
				session.setAttribute("EnemyPlayer", Users.users.get(name));		
				response.sendRedirect("EnemyHumanCastle.jsp");
				break;
			}
		}		
	}
	
	public static <A> User getEnemyUser() {
		@SuppressWarnings("unchecked")
		List<A> keysAsArray = new ArrayList<A>((Collection<? extends A>) Users.users.keySet());
		Random r = new Random();
		return Users.users.get(keysAsArray.get(r.nextInt(keysAsArray.size())));
	}
}
